package ${package}.service.impl;

import ${package}.pojo.${entity};
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
<#if childTable??>
import ${package}.pojo.VO.${entity}VO;
import org.springframework.beans.factory.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.*;
<#list childTable as item>
import ${package}.service.I${item.tableName}Service;
import ${package}.pojo.${item.tableName};
</#list>
</#if>

import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${package}.mapper.${entity}Mapper;
import ${package}.service.I${entity}Service;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.sys.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.hifar.plat.officUtil.ExcelTemplateUtil;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import org.apache.commons.lang3.StringUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.fw.utils.id.IdUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
public class ${entity}ServiceImpl extends ServiceImpl<${entity}Mapper, ${entity}> implements I${entity}Service{

    <#if childTable??>

       <#list childTable as item>
        @Autowired
        private I${item.tableName}Service  ${item.tableName?uncap_first}Service;


       </#list>


        @Override
        @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
        public void save${entity}(${entity}VO ${entity?uncap_first}VO){
            ${entity} ${entity?uncap_first}New = new ${entity}();
            BeanUtils.copyProperties(${entity?uncap_first}VO, ${entity?uncap_first}New);
            // 保存主表
            super.save(${entity?uncap_first}New);
            //  获取主表id
            String  mainId = ${entity?uncap_first}New.getId();
            <#list childTable as item>
            // 保存子表
            List<${item.tableName}> ${item.tableName?uncap_first}List = ${entity?uncap_first}VO.get${item.tableName}List();
            if (${item.tableName?uncap_first}List != null && ${item.tableName?uncap_first}List.size() > 0){
                for (${item.tableName} item : ${item.tableName?uncap_first}List){
                    item.setId(null);
                    item.set${item.fieldName}(mainId);
                }
            }
            ${item.tableName?uncap_first}Service.saveBatch(${item.tableName?uncap_first}List);
            </#list>
        }



        @Override
        @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
        public void edit${entity}(${entity}VO ${entity?uncap_first}VO){
            ${entity} ${entity?uncap_first}New = new ${entity}();
            BeanUtils.copyProperties(${entity?uncap_first}VO, ${entity?uncap_first}New);
            super.updateById(${entity?uncap_first}New);
            //  获取主表id
            String  mainId = ${entity?uncap_first}New.getId();
            // 设置删除条件
            Map<String, Object> columnMap = new HashMap<>();
            <#list childTable as item>

            // 根据主表id删除子表数据
            columnMap.put("${item.field_name}", mainId);
            ${item.tableName?uncap_first}Service.removeByMap(columnMap);
            // 保存子表
            List<${item.tableName}> ${item.tableName?uncap_first}List = ${entity?uncap_first}VO.get${item.tableName}List();
            if (${item.tableName?uncap_first}List != null && ${item.tableName?uncap_first}List.size() > 0){
                for (${item.tableName} item : ${item.tableName?uncap_first}List){
                    item.setId(null);
                    item.set${item.fieldName}(mainId);
                }
            }
            ${item.tableName?uncap_first}Service.saveOrUpdateBatch(${item.tableName?uncap_first}List);

            </#list>
        }


        @Override
        public ${entity}VO query${entity}VO(String mainId){
            ${entity}VO ${entity?uncap_first}VO = new ${entity}VO();
            ${entity} ${entity?uncap_first} = this.getById(mainId);
            BeanUtils.copyProperties(${entity?uncap_first}, ${entity?uncap_first}VO);
            <#list childTable as item>
            List<${item.tableName}> ${item.tableName?uncap_first}List = ${item.tableName?uncap_first}Service.list(new QueryWrapper<${item.tableName}>().eq("${item.field_name}", mainId));
            ${entity?uncap_first}VO.set${item.tableName}List(${item.tableName?uncap_first}List);
            </#list>
            return ${entity?uncap_first}VO;
        }
    </#if>
	 
	<#if table.tableType == '2' >
		 @Override
		 public BusiResponse exportExcel(Map<String, Object> pageInfo) {
			BusiResponse busiResponse = new BusiResponse();
			String ids = (String)pageInfo.get("ids");
			String mainId = (String)pageInfo.get("mainId");
			String operationType = (String)pageInfo.get("operationType");
				       
			if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))){
				ExcelTemplateUtil.exportExcelTemplate(busiResponse, ${entity}.class, "导入模板", "${table.comment}导入数据");
				return busiResponse;
			}else {
				QueryWrapper<${entity}> postParam = RequestConver.getPostParam(pageInfo);
				postParam.ne("del_flag", 1);
				postParam.eq("${fieldNameDbSubTable}", mainId);
				postParam.orderByDesc("create_time");


				if (StringUtils.isNotBlank(ids)) {
				    postParam.in("id", ids);
				}
				List<${entity}> dataList = this.list(postParam);
				 if(dataList!=null && dataList.size()>0) {
					  ExcelTemplateUtil.exportExcel(busiResponse, dataList, ${entity}.class, "${table.comment}数据", "${table.comment}导出");
				}else {
					ExcelTemplateUtil.exportExcelTemplate(busiResponse, ${entity}.class, "${table.comment}数据", "${table.comment}导出");
				}
				return busiResponse;
			}
		}
			    
		@Override
		@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
		public BusiResponse importExcel(MultipartFile file,String mainId) throws Exception {
		    if (file != null && !file.isEmpty()) {
		        List<${entity}> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), ${entity}.class);
		        List<${entity}> importExcelList=new ArrayList<${entity}>();
		        if (importExcel != null && importExcel.size() > 0) {
		                for (${entity} bean : importExcel) {
		                	bean.setId(IdUtils.uuid32());
		                	bean.setDelFlag(0);
		                	bean.set${fieldNameSubTable}(mainId);
		                	//if(!bean.getName().equals("")) {
		                		importExcelList.add(bean);
		                	//}
		                }
		                this.saveBatch(importExcelList);
		             return BusiResponse.ok("导入成功！");
		        }
		    }
		    return BusiResponse.error("导入失败，原因未知！");
		}
    <#else>
		@Override
		public BusiResponse exportExcel(Map<String, Object> pageInfo) {
		    BusiResponse busiResponse = new BusiResponse();
		    String ids = (String)pageInfo.get("ids");

		    String operationType = (String)pageInfo.get("operationType");

		    if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))){
		        ExcelTemplateUtil.exportExcelTemplate(busiResponse, ${entity}.class, "导入模板", "${table.comment}导入数据");
		        return busiResponse;
		    }else {
		        QueryWrapper<${entity}> postParam = RequestConver.getPostParam(pageInfo);
		        postParam.ne("del_flag", 1);
		        postParam.orderByDesc("create_time");
		        if (StringUtils.isNotBlank(ids)) {
		            postParam.in("id", ids);
		        }
		        List<${entity}> dataList = this.list(postParam);
		         if(dataList!=null && dataList.size()>0) {
		        	  ExcelTemplateUtil.exportExcel(busiResponse, dataList, ${entity}.class, "${table.comment}数据", "${table.comment}导出");
		        }else {
		        	ExcelTemplateUtil.exportExcelTemplate(busiResponse, ${entity}.class, "${table.comment}数据", "${table.comment}导出");
		        }
		        return busiResponse;
		    }

		}

		@Override
		@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
		public BusiResponse importExcel(MultipartFile file) throws Exception {
		    if (file != null && !file.isEmpty()) {
		        List<${entity}> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), ${entity}.class);
		        List<${entity}> importExcelList=new ArrayList<${entity}>();
		        if (importExcel != null && importExcel.size() > 0) {
		                for (${entity} bean : importExcel) {
		                	bean.setId(IdUtils.uuid32());
		                	bean.setDelFlag(0);
		                	//if(!bean.getName().equals("")) {
		                		importExcelList.add(bean);
		                	//}
		                }
		                this.saveBatch(importExcelList);
		             return BusiResponse.ok("导入成功！");
		        }
		    }
		    return BusiResponse.error("导入失败，原因未知！");
		}
    </#if>  
	 
}
